package cn.kgc.one.controller;

import cn.kgc.one.pojo.one_User;
import cn.kgc.one.service.One_UserService;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class One_UserController {

    @Resource
    One_UserService one_userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String usercode, String password, Model model, HttpSession session){
        one_User user=one_userService.getuserandpassword(usercode,password);
        if (user==null){
            model.addAttribute("msg","用户名或密码输入有误,请重新输入");
            return "login";
        }else{
            return "231";
        }

    }

    @RequestMapping("/sendCode")
    public Map<String, Object> login(String regist_phone, HttpSession session) {
        Map<String,Object> map=new HashMap<>();
        int code=(int)((Math.random()*9+1)*100000);
        session.setAttribute("code",code);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FgCsA6PRZZ3XScB8w9t", "VHMYGoytJ9TdT4SCcx8XFBQ05H5cwn");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", regist_phone);
        request.putQueryParameter("SignName", "codesendtion");
        request.putQueryParameter("TemplateCode", "SMS_182415416");
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            map.put("msg","OK");
        } catch (com.aliyuncs.exceptions.ServerException e){
            e.printStackTrace();
            map.put("msg",e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            map.put("msg",e.getMessage());
        }
        return map;
    }

    //验证注册
    @RequestMapping("/logins")
    @ResponseBody
    public Map<String,Object> login1(String regist_vcode, HttpSession session,one_User user){
        Map<String,Object> map=new HashMap<>();
        String oldcode=session.getAttribute("code").toString();
        System.out.println(oldcode.equals(regist_vcode));
        if(oldcode.equals(regist_vcode)){
            map.put("msg","ok");
            one_userService.addUser(user);
        }else{
            map.put("msg","验证码错误");

        }
        return map;
    }

}
